//活动轮播
var activitySwiper = new Swiper('.activity-swiper', {
		freeMode : true,
		slidesPerView:'4',
})
//点击标题
$(".myheader").on('tap','.title',function(){
	var $this= $(this);
	if($this.hasClass("slided")){
		$this.removeClass("slided");
		$this.find(".slide-down").css("transform","rotate(180deg)");
	}else{
		$this.addClass("slided");
		$this.find(".slide-down").css("transform","rotate(270deg)");
	}
	$(".slide-container").slideToggle('fast');
})
window.onscroll = function(){
	//懒加载
	lazyLoad($(".lazy[data-load='false']"));
}
//点击上拉
$(".slide-container").on("click","a",function(){
	$(".myheader .title").find("span").html($(this).html())
	$(".slide-container").slideToggle('fast');
	$(".myheader").find(".title").removeClass("slided").find(".slide-down").css("transform","rotate(180deg)");
})
//点击分类
$(".classify").on('tap',function(){
	$(".classify-container").before("<div class='shadow'></div>").css("right","0");
	$("body").css("overflow-y","hidden");
	clickShadow(function(){
		$(".classify-container").css("right","-6rem");
		$("body").css("overflow-y","auto");
	})
})
//点击取消
$(".no").on('tap',function(){
	$(".classify-container").css("right","-6rem");
	$(".shadow").remove();
	$("body").css("overflow-y","auto");
})
//秒杀
$(".item_list").find(".rush").each(function(){
	var id  = $(this).attr("id");
	new timer().showTime({
		container_id:'#'+id,
		start:parseInt($("#"+id).find(".time-container").attr("data-start")),
		now:parseInt($("#"+id).find(".time-container").attr("data-now")),
		end:parseInt($("#"+id).find(".time-container").attr("data-end")),
		callback:function(){
			console.log(this.container_id);
		$(this.container_id).fadeOut(function(){
			$(this).remove();
			
		})
	}
	}).countDown();
})
//一级分类
var typeSwiper = new Swiper('.type-swiper', {
		freeMode : true,
		slidesPerView:'2',
		preventClicksPropagation:true,
		initialSlide :$(".type-swiper").find(".active").parents("li").index(),
})
//点击一级分类
$(".type-swiper").on("click","span",function(){
	if($(this).hasClass("active")){             
		$(this).removeClass("active");
		$(".second-type-list").empty();
	}else{
		$(".type-swiper").find("span.active").removeClass("active");
		$(this).addClass("active");
		var firstSellerProductCategoryId = $(this).parent("a").attr("data-firstSellerProductCategoryId");
		$.ajax({
			type:"get",
			url:base_url+"customer/getSecondSellerProductCategoryByFirstSellerProductCategoryId",
			data:{"firstSellerProductCategoryId":firstSellerProductCategoryId},
			async:false,
			success:function(data){
				$(".second-type-list").empty();
				var html = "";
				for(var i=0;i<data.length;i++){
					html = html + 
					'<span class="second-type floatLeft" data-secondSellerProductCategoryId="'+
					data[i].sellerProductCategoryId+'">'+
					data[i].categoryName+'</span>';

				}
				$(".second-type-list").append(html);
				textAuto('second-type',4);
			},
			error:function(){
			}
		});
	}
})
//点击二级分类
$(".second-type-list").on("click",".second-type",function(){
	var $this = $(this);
	if($this.hasClass("active")){
		$this.removeClass("active");
	}else{
		$(".second-type-list").find(".second-type.active").removeClass("active");
		$this.addClass("active");
	}
})
//字体截取
function textAuto(thisText,needLeng){
	 var textList = document.getElementsByClassName(thisText);
	 for (var i=0;i<textList.length;i++) {
 		var nowLeng = textList[i].innerHTML.length;
 		if(nowLeng > needLeng){
	  		var nowWord = textList[i].innerHTML.substr(0,needLeng)+'...';
	  		textList[i].innerHTML = nowWord;
		 }
	 }
}
/**
 * 点击确认键进行查询
 */
$(".yes").click(function(){
	var productType = $("#productTypeMessage").attr("data-productType");
	var activityName = $("#productTypeMessage").attr("data-activityName");
	var sellerId = $("#productTypeMessage").attr("data-sellerId");
	var firstSellerProductCategoryId =  $(".type-swiper").find("span.active").parent("a").attr("data-firstSellerProductCategoryId");
	var secondSellerProductCategoryId = $(".second-type-list").find(".second-type.active").attr("data-secondSellerProductCategoryId");
	if(firstSellerProductCategoryId!=null&&firstSellerProductCategoryId!=""){
		if(secondSellerProductCategoryId!=null&&secondSellerProductCategoryId!=""){
			var json = {
					"productType":productType,
					"activityName":activityName,
					"sellerId":sellerId,
					"firstSellerProductCategoryId":firstSellerProductCategoryId,
					"secondSellerProductCategoryId":secondSellerProductCategoryId,
					"searchType":""
			}
			var jsonStr = JSON.stringify(json);
			window.location.href = base_url + "customer/getShopActivityClassifyMsg?jsonStr="+jsonStr;
		}else{
			var json = {
					"productType":productType,
					"activityName":activityName,
					"sellerId":sellerId,
					"firstSellerProductCategoryId":firstSellerProductCategoryId,
					"searchType":""
					}
			var jsonStr = JSON.stringify(json);
			window.location.href = base_url + "customer/getShopActivityClassifyMsg?jsonStr="+jsonStr;
		}
	}else{
		var json = {
				"productType":productType,
				"activityName":activityName,
				"sellerId":sellerId,
				"searchType":""
		}
		var jsonStr = JSON.stringify(json);
		window.location.href = base_url + "customer/getShopActivityClassifyMsg?jsonStr="+jsonStr;
	}
	
});
//点击活动
$(".activity").on("click",function(){
	$(this).find(".activity-list").slideToggle('fast');
})

//页面加载的时执行字体截取
//注意了，如果二级分类是ajax出来的，铺完数据之后也要执行这个函数；
textAuto('first-type',5);
textAuto('second-type',4);
